<?php
require_once ("utils/MySQL.php");
$db = MySQL::getInstance();
//输出的文件类型为excel
header("Content-type:application/vnd.ms-excel");
//提示下载
header("Content-Disposition:attachement;filename=user_answer".date("Ymd").".xls");
//报表数据
//查询用户答题数据
$sql = "select * from wx_vote as wv LEFT JOIN wx_vote_info wvi on wv.id = wvi.vote_id right JOIN wx_vote_answer as wva on wvi.id = wva.vote_info_id";
$result = $db->fetchAll($sql,2);
$ReportArr = array();
$ReportArr[] = array("投票标题","投票的题目","投票的选项","用户名","用户的选择");
if($result){
    for($i=0;$i<count($result);$i++){
        $r = $result[$i];
        $ReportArr[] = array($r["title"],$r["subject"],$r["answer_option"],$r["username"],$r["answer"]);
    }
}
//$ReportArr = array(array(1,2,3,4,5),
//    array('A','B','C','D','E'),
//    array('up','down','left','right','center'),
//    array('欢','迎','光','临','小蛤蟆腿','的','CSDN','博客')
//);
$ReportContent = '';
$num1 = count($ReportArr);
for($i=0;$i<$num1;$i++){
    $num2 = count($ReportArr[$i]);
    for($j=0;$j<$num2;$j++){
        //ecxel都是一格一格的，用\t将每一行的数据连接起来
        $ReportContent .= '"'.$ReportArr[$i][$j].'"'."\t";
    }
    //最后连接\n 表示换行
    $ReportContent .= "\n";
}
//用的utf-8 最后转换一个编码为gb
$ReportContent = mb_convert_encoding($ReportContent,"gb2312","utf-8");
//输出即提示下载
echo $ReportContent;
?>